package com.pet.server.ext.util;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;

import com.pet.server.ext.Config;

//import com.pet.server.ext.Config;


import java.sql.PreparedStatement;

import redis.clients.jedis.Jedis;

public class DataAccessor {

    public static DataAccessor createInstance() {
        return new DataAccessor();
    }

//    public static void init(String serverName) {
//        if (DataAccessor.serverName == null) {
//            DataAccessor.serverName = serverName;
//        }
//    }
    
//    private static String serverName = null;

    private String connName, driverName, username, password;
    
    public Connection getConnection() {
        while (connection == null && tryConnectTime < 3) {
            try{
                // count
                tryConnectTime ++;
                
                // init parameters
//                if (serverName != null && serverName.endsWith("duapp.com")) {
//                    String databaseName = "qiHYNrbMpkidAvOZoJye";
//                    String host = "sqld.duapp.com";
//                    String port = "4050";
//                    username = "1lVfQhlLfOmOh24w2f8uBNRU";
//                    password = "4LKk2abyiLDl3sMPRSjV14qgeYedxSZG";
//                    driverName = "com.mysql.jdbc.Driver";
//                    String dbUrl = "jdbc:mysql://";
//                    String serverName = host + ":" + port + "/";
//                    connName = dbUrl + serverName + databaseName;
//                }else{
                    connName = Config.getValue("jdbc.url");  //"jdbc:mysql://182.92.172.235:3306/huaqianshu";
                    driverName = Config.getValue("jdbc.driverClassName"); //"com.mysql.jdbc.Driver";
                    username = Config.getValue("jdbc.username"); //"huaqianshu";
                    password = Config.getValue("jdbc.password");//"hua10001qaz2wsx";
//                }
//                String databaseName = "qiHYNrbMpkidAvOZoJye";
//                String host = "sqld.duapp.com";
//                String port = "4050";
//                String username = "1lVfQhlLfOmOh24w2f8uBNRU";
//                String password = "4LKk2abyiLDl3sMPRSjV14qgeYedxSZG";
//                String driverName = "com.mysql.jdbc.Driver";
//                String dbUrl = "jdbc:mysql://";
//                String serverName = host + ":" + port + "/";
//                String connName = dbUrl + serverName + databaseName;
                // connect
//                String connName=Config.getValue("jdbc.url");  //"jdbc:mysql://182.92.172.235:3306/huaqianshu";
//                String driverName =Config.getValue("jdbc.driverClassName"); //"com.mysql.jdbc.Driver";
//                String username=Config.getValue("jdbc.username"); //"huaqianshu";
//                String password=Config.getValue("jdbc.password");//"hua10001qaz2wsx";
                
                Class.forName(driverName);
                connection = DriverManager.getConnection(connName, username, password);
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
    
    public Jedis getJedis() {
    	return null;
    }
    
    public void closeAll() {
        // close all the Statement and Jedis instances here
    	if (resultSet != null) {
    		try {
    			resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    	}
    	if (pStatement != null) {
    		try {
    			pStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    	}
    	if (statement != null) {
    		try {
    			statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    	}
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public ResultSet executeQuery(String sql) throws SQLException{
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (statement == null) {
    		Connection con = getConnection();
    		statement = con.createStatement();
    	}
    	resultSet = statement.executeQuery(sql);
    	return resultSet;
    }
    
    public ResultSet executeQuery(PreparedSql pSql) throws SQLException{
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (pStatement != null) {
    		pStatement.close();
    		pStatement = null;
    	}
    	Connection con = getConnection();
    	pStatement = pSql.createPreparedStatement(con);
    	resultSet = pStatement.executeQuery();
    	return resultSet;
    }

    public int executeUpdate(PreparedSql pSql) throws SQLException{
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (pStatement != null) {
    		pStatement.close();
    		pStatement = null;
    	}
    	Connection con = getConnection();
    	pStatement = pSql.createPreparedStatement(con);
    	int ret = pStatement.executeUpdate();
    	pStatement.close();
    	pStatement = null;
    	return ret;
    }

    public int executeUpdate(String sql) throws SQLException{
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (statement == null) {
    		Connection con = getConnection();
    		statement = con.createStatement();
    	}
    	int ret = statement.executeUpdate(sql);
    	return ret;
    }

    public int executeUpdate4Key(PreparedSql pSql) throws SQLException{
    	int ret = -1;
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (pStatement != null) {
    		pStatement.close();
    		pStatement = null;
    	}
    	Connection con = getConnection();
    	pStatement = pSql.createPreparedStatement4Key(con);
    	int row = pStatement.executeUpdate();
    	if (row > 0) {
    		ResultSet rs = pStatement.getGeneratedKeys();
    		if (rs != null) {
    			if (rs.next()) {
    				ret = rs.getInt(1);
    			}
    			rs.close();
    		}
    	}
    	pStatement.close();
    	pStatement = null;
    	return ret;
    }

    public int executeUpdate4Key(String sql) throws SQLException{
    	int ret = -1;
    	if (resultSet != null) {
    		resultSet.close();
    		resultSet = null;
    	}
    	if (statement == null) {
    		Connection con = getConnection();
    		statement = con.createStatement();
    	}
    	int row = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
    	if (row > 0) {
    		ResultSet rs = pStatement.getGeneratedKeys();
    		if (rs != null) {
    			if (rs.next()) {
    				ret = rs.getInt(1);
    			}
    			rs.close();
    		}
    	}
    	return ret;
    }
    
    private ResultSet resultSet = null;
    private PreparedStatement pStatement = null;
    private Statement statement = null;
    private Connection connection = null;
    private int tryConnectTime = 0;
}
